package com.dreamstations.changshads.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.dreamstations.changshads.R;
import com.dreamstations.changshads.activity.ArticleDetailActivity;
import com.dreamstations.changshads.activity.ArticleDetail_WebActivity;
import com.dreamstations.changshads.adapter.DSRvAdapter;
import com.dreamstations.changshads.entity.InvestigateBean;
import com.dreamstations.changshads.util.DSRequest;
import com.dreamstations.changshads.util.RequestType;
import com.squareup.picasso.Picasso;
import com.zhy.base.adapter.ViewHolder;
import com.zhy.base.adapter.recyclerview.OnItemClickListener;

// TODO: 2016/6/26 史实考证是否需要用通用的布局文件？
public class Investigate_Fact_Frag extends BaseFragmentForHome<InvestigateBean> {
    private final String TAG = Investigate_Fact_Frag.class.getName();

    public Investigate_Fact_Frag() {
    }

    @SuppressLint("ValidFragment")
    public Investigate_Fact_Frag(String title, @RequestType String reqType, int category) {
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        bundle.putString("reqType", reqType);
        bundle.putInt(PARAM_TYPEID, category);
        setArguments(bundle);

    }


    @SuppressWarnings("WrongConstant")
    @NonNull
    @Override
    public DSRequest getDsRequest() {
        Bundle bundle = getArguments();
        return new DSRequest()
                .type(bundle.getString("reqType"))
                //二级分类
                .addParams(PARAM_TYPEID, String.valueOf(bundle.getInt(PARAM_TYPEID)))
                .setTag(Investigate_Fact_Frag.this);
    }

    @NonNull
    @Override
    public DSRvAdapter.RvSupport<InvestigateBean> getItem() {
        return new DSRvAdapter.RvSupport<InvestigateBean>() {
            @Override
            public int getItemViewType(InvestigateBean data) {
                if (data.getTemplate() == 1) return 0;
                return 1;
            }

            @Override
            public ViewHolder createHolder(Context context, ViewGroup parent, int viewType) {
                final ViewHolder holder = ViewHolder.get(context, null, parent, R.layout.investigate_item2, -1);
                return holder;
            }

            @Override
            public void convert(ViewHolder holder, InvestigateBean data) {
                holder.setText(R.id.item_title, data.getTitle());
                holder.setText(R.id.item_time, data.getTime());
                ImageView imageView = holder.getView(R.id.item_cover);
                Picasso.with(getActivity())
                        .load(DSRequest.SERVER_BASE1 + data.getCover())
                        .into(imageView);
            }
        };
    }

    @Override
    public OnItemClickListener getOnItemClickListener() {
        return new OnItemClickListener<InvestigateBean>() {
            @Override
            public void onItemClick(ViewGroup parent, View view, InvestigateBean data, int position) {
                Intent intent = new Intent();
                if (data.getTemplate() == 1) {
                    intent.setClass(getActivity(), ArticleDetail_WebActivity.class);
                } else {
                    intent.setClass(getActivity(), ArticleDetailActivity.class);
                }
                intent.putExtra("data", data);
//                L.i(TAG,"=>dataId:"+data.getId());
                startActivity(intent);

            }

            @Override
            public boolean onItemLongClick(ViewGroup parent, View view, InvestigateBean o, int position) {
                return false;
            }
        };
    }

    @Override
    public DSRvAdapter.RvSupport getHeader() {
//        Picasso picasso = Picasso.with(getContext());
//        final List<ImageView> imageViews = new ArrayList<ImageView>();
//        for (int i = 0; i < 6; i++) {
//            ImageView imageView = new ImageView(this.getActivity());
//            // 异步加载图片
//            picasso.load("http://i4.buimg.com/a43ec188b9ac79d0s.png").into(imageView);
//            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//            imageViews.add(imageView);
//        }
//        DSRvAdapter.RvSupport header = new DSRvAdapter.RvSupport() {
//            @Override
//            public int getItemViewType(Object data) {
//                return 0;
//            }
//
//            @Override
//            public ViewHolder createHolder(Context context, ViewGroup parent, int viewType) {
//                ViewHolder holder = ViewHolder.get(context, null, parent, R.layout.vpheader, -1);
//                ViewPager viewPager = holder.getView(R.id.header_viewpager);
//                Slide_Indicator indicator = holder.getView(R.id.header_indicator);
//                PagerAdapter pagerAdapter = new PagerAdapter() {
//                    @Override
//                    public int getCount() {
//                        return imageViews.size();
//                    }
//
//                    @Override
//                    public boolean isViewFromObject(View view, Object object) {
//                        return view == object;
//                    }
//
//                    @Override
//                    public Object instantiateItem(ViewGroup container, int position) {
//                        container.addView(imageViews.get(position));
//                        return imageViews.get(position);
//                    }
//
//                    @Override
//                    public void destroyItem(ViewGroup container, int position, Object object) {
//                        container.removeView(imageViews.get(position));
//                    }
//                };
//                viewPager.setAdapter(pagerAdapter);
//                indicator.setupWithViewPager(viewPager);
//                return holder;
//            }
//
//            @Override
//            public void convert(ViewHolder holder, Object data) {
//
//            }
//
//        };
//        return header;
        return null;
    }

    @Override
    public boolean getEnablePullLoadMore() {
        return true;
    }
}
